//app.js
import WxService from './assets/plugins/wx-service/WxService'
import HttpService from './helpers/HttpService'
import __config from './config/config'
var md5 = require('utils/md5.js')
App({
  onLaunch: function(option) {
    // 小程序模板消息1014
    console.log("[onLaunch] 本次场景值:", option.scene)
    if (option.scene==1008){
      wx.setStorageSync('xfc', 1)
    }
    if (wx.cloud) {
      wx.cloud.init({
        traceUser: true
      })
    }
    wx.getSystemInfo({
      success: e => {
        console.log(e, "6552452222222222222222222222222222")
        this.globalData.StatusBar = e.statusBarHeight;
        let custom = wx.getMenuButtonBoundingClientRect();
        this.globalData.Custom = custom;
        this.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
      }
    })
  



    // 获取小程序更新机制兼容
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function(res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function() {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function(res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function() {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
            })
          })
        }
      })
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
    var appId = "wxca4d65358a45f1f4";
    this.data.appId = appId;
    this.getUserInfo()
  },
  getUserInfo() {
    let that = this
    var openId = (wx.getStorageSync('openid'))
    // 如果有openId 就执行getUserInfo 没有授权过就会返回失败

    if (openId) {
      wx.getUserInfo({
        success: function(res) {
     


          let userInfo = res.userInfo
          userInfo.openId = openId

          let userParams = {
            "encryptedData": res.encryptedData,
            "rawData": res.rawData,
            "signature": res.signature,
            "iv": res.iv,
            "sessionKey": wx.getStorageSync('sessionKey')
          }
        

          that.HttpService.checkUserInfo(userParams).then(res => {

            console.log(res, "j检测1");
          })
          that.HttpService.saveUser(userInfo).then(res => {
            console.log(res, "检测2")
          
          }, error => {
            console.log(error)
          })
        },
        fail: function(res) {

          that.HttpService.kaiguan().then(ccc => {
            console.log(ccc, "开关的返回值");
            var xian = ccc.data.data
            if (xian == 1) {
              wx.showModal({
                title: '警告',
                content: '尚未进行授权，请点击确定跳转到授权页面进行授权。',
                success: function(res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '/pages/login/index',
                    })
                  }
                }
              })
            } else {
              console.log("开关为关闭状态")
            }
          })

          console.log("获取失败getUserInfo！")



        },
        complete: function() {
          // console.log("获取用户信息完成getUserInfo！")
        }
      })
    } else {
      that.login()
    }
  },
  login() {
    let that = this
    wx.login({
      success: function(res) {
        if (res.code) {
          that.HttpService.login({
            code: res.code
          }).then(res => {
            console.log( "首次请求是否老用户",  res)
       
            let data = res.data.data

            if (res.data.code == 200) {
              wx.setStorageSync('openid', data.openid)
              wx.setStorageSync('userId', data.userId)
              wx.setStorageSync('sessionKey', data.sessionKey)
              wx.setStorageSync('dengl', data.isNew)


            } else {
     
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                showCancel: false
              })
            }
          })
          //控制首页是否授权
          //请求开关的接口
          that.HttpService.kaiguan().then(ccc => {
            console.log(ccc, "开关的返回值");
            var xian = ccc.data.data
            if (xian == 1) {
              wx.navigateTo({
                url: '/pages/login/index',
              })
            } else {
              console.log("戏啊白案开关为关闭状态")
            }
          })


          
        }
      }
    })
  },
  onShow: function() {

  },
  onHide: function() {

  },
  data: {
    userInfo: null,
    appId: null
  },
  globalData: {
    ColorList: [{
      title: '嫣红',
      name: 'red',
      color: '#e54d42'
    },
    {
      title: '桔橙',
      name: 'orange',
      color: '#f37b1d'
    },
    {
      title: '明黄',
      name: 'yellow',
      color: '#fbbd08'
    },
    {
      title: '橄榄',
      name: 'olive',
      color: '#8dc63f'
    },
    {
      title: '森绿',
      name: 'green',
      color: '#39b54a'
    },
    {
      title: '天青',
      name: 'cyan',
      color: '#1cbbb4'
    },
    {
      title: '海蓝',
      name: 'blue',
      color: '#0081ff'
    },
    {
      title: '姹紫',
      name: 'purple',
      color: '#6739b6'
    },
    {
      title: '木槿',
      name: 'mauve',
      color: '#9c26b0'
    },
    {
      title: '桃粉',
      name: 'pink',
      color: '#e03997'
    },
    {
      title: '棕褐',
      name: 'brown',
      color: '#a5673f'
    },
    {
      title: '玄灰',
      name: 'grey',
      color: '#8799a3'
    },
    {
      title: '草灰',
      name: 'gray',
      color: '#aaaaaa'
    },
    {
      title: '墨黑',
      name: 'black',
      color: '#333333'
    },
    {
      title: '雅白',
      name: 'white',
      color: '#ffffff'
    },
    ]

  },

  HttpService: new HttpService({
    baseURL: __config.basePath
  }),
  WxService: new WxService,
  __config
})